package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.entity.Brand;
import com.cskaoyan.utils.BaseRespVoUtil;
import com.cskaoyan.bean.vo.BasePageData;
import com.cskaoyan.bean.vo.BaseParam;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  品牌制造商Controller
 *
 */
@RestController
@RequestMapping("admin/brand")
public class BrandController {

    @Autowired
    BrandService brandService;

    /**
     *  根据条件查询指定品牌的handler方法
     *
     */
    @RequestMapping("list")
    public BaseRespVo query(Integer id,String name,BaseParam baseParam){
        BasePageData<Brand> brands = brandService.query(id,name,baseParam);
        return BaseRespVo.ok(brands);
    }


    /**
     *
     *  添加品牌制造商
     */
    @RequestMapping("create")
    public BaseRespVo add(@RequestBody Brand brandReceive){
        Brand brandResp = brandService.add(brandReceive);
        // 查询之后返回，使id有值
        Brand brand = read(brandResp);
        return BaseRespVo.ok(brand);
    }

    /**
     *  read方法，用于在add方法执行后给id赋值
     *
     */
    private Brand read(Brand brand){
        return brandService.read(brand);
    }


    /**
     *  修改、更新数据
     *
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Brand updatedBrand){
        Brand brandResp = brandService.update(updatedBrand);
        return BaseRespVo.ok(brandResp);

    }

    /**
     *  删除数据
     *
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Brand brand){
        brandService.delete(brand);
        return BaseRespVo.ok();
    }
}
